package cn.edu.web;

import cn.edu.pojo.Book;
import cn.edu.pojo.Collection;
import cn.edu.pojo.ShowObj;
import cn.edu.pojo.Student;
import cn.edu.service.BookService;
import cn.edu.service.CollectionService;
import cn.edu.service.impl.BookServiceImpl;
import cn.edu.service.impl.CollectionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MonsterServlet extends BaseServlet{
    CollectionService collectionService = new CollectionServiceImpl();
    BookService bookService = new BookServiceImpl();
    List<ShowObj> showObjs = new ArrayList<>();
    /**
     * 添加收藏的图书信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sn = req.getParameter("sn");
        Book book = bookService.getBookBySn(sn);
        Object user = req.getSession().getAttribute("user");
        if (user instanceof Student){
            Student student = (Student)user;
            int i = collectionService.addCollection(new Collection(student.getSno(), book.getSn(), null, new Date()));
            resp.sendRedirect(req.getContextPath()+"/bookServlet?action=allBook");
        }
    }

        /**
         * 移除相应已经收藏的图书信息图书的信息
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void deleteBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sn = req.getParameter("sn");
        int i = collectionService.deleteCollection(sn);
        System.out.println(i);
        resp.sendRedirect(req.getContextPath()+"/monsterServlet?action=allBook");
    }

        /**
         * 展示所有的当前用户已经收藏的图书信息表
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void allBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        showObjs.clear();
        HttpSession session = req.getSession();
        Student student=null;
        Object user = session.getAttribute("user");
        if (user instanceof Student){
            student= (Student)user;
        }
        List<Collection> collections = collectionService.allCollection(student.getSno());
        for (Collection collection:collections){
            Book bookBySn = bookService.getBookBySn(collection.getSn());
            ShowObj showObj = new ShowObj(bookBySn.getSn(),collection.getLabel(),collection.getDate(),bookBySn.getBname());
            showObjs.add(showObj);
        }
        req.setAttribute("showObj",showObjs);
        req.getRequestDispatcher("/moster.jsp").forward(req,resp);
    }
}
